package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;


@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    CustomerRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Override
    public void run(String... strings) throws Exception {
        // save a couple of customers
        repository.save(new Customer("Jack", "Bauer"));
        repository.save(new Customer("Jack", "Lai"));
        repository.save(new Customer("Chloe", "O'Brian"));
        repository.save(new Customer("Kim", "Bauer"));
        repository.save(new Customer("David", "Palmer"));
        repository.save(new Customer("Michelle", "Dessler"));
        repository.save(new Customer("Jack1", "Bauer"));
        repository.save(new Customer("Jack2", "Lai"));
        repository.save(new Customer("Chloe3", "O'Brian"));
        repository.save(new Customer("Kim4", "Bauer"));
        repository.save(new Customer("David5", "Palmer"));
        repository.save(new Customer("Michelle6", "Dessler"));
        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        Pageable page= new PageRequest(2,3);
        for (Customer customer : repository.findAll(page)) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer by ID
        Customer customer = repository.findOne(1L);
        System.out.println("Customer found with findOne(1L):");
        System.out.println("--------------------------------");
        System.out.println(customer);
        System.out.println();
        repository.setLastNameFor("Lai",1L);
        customer = repository.findOne(1L);
        System.out.println("--------------------------------");
        System.out.println(customer);
        System.out.println();

        // fetch customers by last name
        System.out.println("Customer found with findByLastName('Bauer'):");
        System.out.println("--------------------------------------------");
        for (Customer bauer : repository.findByLastName("Bauer")) {
            System.out.println(bauer);
        }

        System.out.println("-----------------------111---------------------");
        for (Customer bauer : repository.findByFirstName("Jack")) {
            System.out.println(bauer);
        }

        System.out.println("-----------------------222---------------------");
        for (Customer bauer : repository.findByFirstNameAndLastName("Michelle", "Lai")) {
            System.out.println(bauer);
        }


    }

}